package com.mapproject.industrymap.controller;
import com.mapproject.industrymap.entity.SysUserEntity;
import com.mapproject.industrymap.service.SysUserService;
import com.mapproject.industrymap.utils.EncodeMd5;
import com.mapproject.industrymap.utils.ReturnValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    SysUserService sysUserService;
    @PostMapping("/create")
    public ReturnValue createUser(@RequestBody SysUserEntity sysUserEntity)throws Exception{
        EncodeMd5 encodeMd5=new EncodeMd5();
        String newpass=encodeMd5.MD5(sysUserEntity.getPassword());
        sysUserEntity.setPassword(newpass);
        return sysUserService.createUser(sysUserEntity);
    }
    @PostMapping("/modify")
    public ReturnValue modifyUser(@RequestParam(value = "ispassword")String ispassword,@RequestBody SysUserEntity sysUserEntity)throws Exception{
        if(ispassword.equals("1")){
            EncodeMd5 encodeMd5=new EncodeMd5();
            String newpass=encodeMd5.MD5(sysUserEntity.getPassword());
            sysUserEntity.setPassword(newpass);
            return sysUserService.modifyUser(sysUserEntity);
        }
        else{
            sysUserEntity.setPassword(null);
            return sysUserService.modifyUser(sysUserEntity);
        }
    }
    @PostMapping("/remove")
    public ReturnValue removeUser(@RequestParam(value = "userid")int userid)throws Exception{
        return sysUserService.removeUser(userid);
    }
}
